// pages/search/search.js
import { Search } from 'search-model.js';
var search = new Search();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchKey: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      searchKey: options.key
    })
    this._loadData();
  },

  _loadData: function (e) {
    var that = this;
    if (!this.data.searchKey) {
      wx.showModal({
        title: '搜索提示',
        content: '请输入宠物ID/昵称',
      })
      return false;
    }
    search.searchPets(this.data.searchKey, (res) => {
      console.log(res)
      that.setData({
        petsData: res
      })
    })
  },

  // 跳转到宠物详情页
  onPetsInfo: function (e) {
    var id = search.getDataSet(e, 'id');
    wx.navigateTo({
      url: '../pets-info/pets-info?id=' + id,
    })
  },

  searchSubmit: function (e) {
    wx.navigateTo({
      url: '../search/search?key=' + this.data.searchKey,
    })
  },

  searchInputEvent: function (e) {
    this.setData({
      searchKey: e.detail.value
    })
  },
})